package deepFirstSearch;

public class MaxAreaOfIsland {

    //辅助方向使用
    private int[] direction = new int[]{1, 0, -1, 0 , 1};

    /**
     * LeetCode T695 岛屿的最大面积
     * @param args
     */
    public static void main(String[] args) {

    }

    //使用递归方法
    public int maxAreaOfIsland(int[][] grid) {
        if (grid.length == 0 || grid[0].length == 0){
            return 0;
        }
        int ans = 0;
        for (int i = 0; i < grid.length; i++){
            for (int j = 0; j < grid[0].length; j++){
                if (grid[i][j] != 0){
                    ans = Math.max(ans, dfs(grid, i, j));
                }

            }
        }
        return ans;
    }

    public int dfs(int[][] grid, int r, int c){
        if (grid[r][c] == 0){
            return 0;
        }
        //进行标记表示已经被遍历过
        grid[r][c] = 0;
        int x, y, area = 1;
        for (int i = 0; i < 4; i++){
            x = r + direction[i];
            y = c + direction[i + 1];
            if (x >= 0 && x < grid.length && y >= 0 && y < grid[0].length){
                area += dfs(grid, x, y);
            }
        }
        return area;
    }
}
